package com.demomaster.quickserialport.sample;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.demomaster.quickserialport.QuickSerialPort;
import com.demomaster.quickserialport.SerialPortClient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    SerialPortClient serialPortClient;
    TextView tv_reciver;
    EditText et_input;

    Handler handler;
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            System.out.println("CPU温度：" + getCpuTemperature() + "℃");
            handler.removeCallbacks(this);
            handler.postDelayed(this, 5000);
        }
    };

    public float getCpuTemperature() {
        String basePath = "/sys/class/thermal/";
        File thermalDir = new File(basePath);
        for (File zone : thermalDir.listFiles()) {
            File typeFile = new File(zone, "type");
            try (BufferedReader br = new BufferedReader(new FileReader(typeFile))) {
                String type = br.readLine().trim();
                if ("soc-thermal".equalsIgnoreCase(type)) {
                    File tempFile = new File(zone, "temp");
                    try (BufferedReader tempBr = new BufferedReader(new FileReader(tempFile))) {
                        String tempStr = tempBr.readLine().trim();
                        return Integer.parseInt(tempStr) / 1000f; // 单位转换为℃
                    }
                }
            } catch (Exception e) {
                // 处理异常
            }
        }
        return -1f; // 未找到有效路径
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_reciver = findViewById(R.id.tv_reciver);
        et_input = findViewById(R.id.et_input);
        findViewById(R.id.btn_send).setOnClickListener(this);
        //serialPortClient = QuickSerialPort.getInstance().openSerialPort("/dev/ttyS4", 9600);
        handler = new Handler();
        handler.post(runnable);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_send) {
            if (serialPortClient == null) {
                try {
                    serialPortClient = QuickSerialPort.getInstance().openSerialPort("/dev/ttyS4", 9600);
                    serialPortClient.setOnDataReceiveListener(new SerialPortClient.IDataRecvListener() {
                        @Override
                        public void onDataReceive(String path, byte[] buffer) {
                            Log.i("收到", new String(path));
                            tv_reciver.setText("收到：" + new String(buffer));
                            //tv_reciver.append(new String(buffer));
                        }
                    });
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            if(serialPortClient!=null) {
                if (!TextUtils.isEmpty(et_input.getText())) {
                    serialPortClient.send(et_input.getText().toString().getBytes());
                }
            }
        }
    }
}